package com.easy.auth.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.Action;
import com.easy.auth.service.ActionService;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("ActionController")
public class ActionController {

	@Autowired
	private ActionService actionService;

	@RequestMapping("goToActionManager")
	public String goToActionManager(String secondMenuId,
			HttpServletRequest request) {
		request.getSession().setAttribute("secondMenuId", secondMenuId);
		return "auth/action";
	}

	/**
	 * 查询全部节点数据
	 * 
	 * @return
	 */
	@RequestMapping("getActionList")
	@ResponseBody
	public PagedList<Action> getActionList() {
		PagedList<Action> pagedList = actionService.getAllActions();
		return pagedList;
	}

	/**
	 * 根据oid查询该节点其他数据
	 * 
	 * @return
	 */
	@RequestMapping("findActionById")
	@ResponseBody
	public Action findActionById(Action action) {
		return actionService.findActionById(action);
	}

	/**
	 * 保存资源信息
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	@RequestMapping("saveActionItem")
	@ResponseBody
	public Message saveTreegridItem(Action action, HttpServletRequest request) {
		int count = actionService.saveAction(action);
		return new SaveOrEditMessage(count);
	}

	/**
	 * 删除资源信息
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	@RequestMapping("removeActionItems")
	@ResponseBody
	public Message removeTreegridItem(String oids) {
		String[] ids = oids.split("-");
		return new DeleteMessage(actionService.removeActions(ids));
	}

	/**
	 * 根据用户id获取对应的资源信息
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping("getActionsByUserId")
	@ResponseBody
	public PagedList<Action> getActionsByUserId(String userid) {
		return actionService.getActionsByUserId(userid);
	}

	/**
	 * 根据用户id更新对应的资源信息
	 * 
	 * @param userid
	 * @param chekcedIds
	 * @return
	 */
	@RequestMapping("updateUserActions")
	@ResponseBody
	public SaveOrEditMessage updateUserActions(String userid, String checkedIds) {
		String[] checkedids = checkedIds.split(",");
		return new SaveOrEditMessage(actionService.updateUserActions(userid,
				checkedids));
	}

	/**
	 * 根据角色id获取对应的资源信息
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping("getActionsByRoleId")
	@ResponseBody
	public PagedList<Action> getActionsByRoleId(String roleid) {
		return actionService.getActionsByRoleId(roleid);
	}

	/**
	 * 根据用户id更新对应的资源信息
	 * 
	 * @param userid
	 * @param chekcedIds
	 * @return
	 */
	@RequestMapping("updateRoleActions")
	@ResponseBody
	public SaveOrEditMessage updateRoleActions(String roleid, String checkedIds) {
		String[] checkedids = checkedIds.split(",");
		return new SaveOrEditMessage(actionService.updateRoleActions(roleid,
				checkedids));
	}

	/**
	 * 根据部门id获取对应的资源信息
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping("getActionsByDeptId")
	@ResponseBody
	public PagedList<Action> getActionsByDeptId(String deptId) {
		return actionService.getActionsByDeptId(deptId);
	}

	/**
	 * 根据部门id更新对应的资源信息
	 * 
	 * @param userid
	 * @param chekcedIds
	 * @return
	 */
	@RequestMapping("updateDeptActions")
	@ResponseBody
	public SaveOrEditMessage updateDeptActions(String deptId, String checkedIds) {
		String[] checkedids = checkedIds.split(",");
		return new SaveOrEditMessage(actionService.updateDeptActions(deptId,
				checkedids));
	}

	@RequestMapping("changeSort")
	@ResponseBody
	public SaveOrEditMessage changeSort(String firstId, String secondId) {
		return new SaveOrEditMessage(
				actionService.changeSort(firstId, secondId));
	}
}
